package com.my.files;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author: marshal
 * @date: 2021-09-27 4:28 PM
 */
public class MyFileUtils {
    private MyFileUtils() {}


    /**
     *  创建文件
     *
     * @param path
     * @return
     */
    public static boolean createFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                return file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("file " + " already exist.");
        return false;
    }

    /**
     *  列出某个目录下的文件
     *
     * @param dirPath
     * @return
     */
    public static String listFiles(String dirPath) {
        var file = new File(dirPath);
        if (!file.exists()) {
            throw new RuntimeException("directory " + dirPath + " not exist.");
        }
        return Arrays.toString(file.list());
    }
}
